package com.kaibes.imserver.control;

import java.io.File;
import java.net.Socket;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.kaibes.imserver.KaibesIMServer;
import com.kaibes.imserver.control.threads.ClientStringReaderLogin;
import com.kaibes.imserver.control.threads.ClientStringReaderMain;
import com.kaibes.imserver.model.ClientStringWriter;
import com.kaibes.imserver.model.StartButtonListenerModel;
import com.kaibes.imserver.model.data.SocketStreamData;
import com.kaibes.imserver.model.data.UserCenterInformationData;
import com.kaibes.imserver.model.database.KaibesimcenterHelper;
import com.kaibes.imserver.model.utils.JsonParserUtil;
import com.kaibes.imserver.model.utils.KaibesFilesUtil;
import com.kaibes.imserver.model.utils.StaticValuesUtil;

public class UserSocketControl {

	private Socket client;

	private UserCenterInformationData userCenterData = null;
	private SocketStreamData socketStreamData;

	private ClientStringWriter clientStringWriter;

	private Integer currentThreadNum = 0;
	private File userFileFolder;
	private File userCacheFolder;

	private KaibesFilesUtil kaibesFilesUtil;

	public UserSocketControl(Socket client) {
		this.client = client;
		this.socketStreamData = new SocketStreamData(client, "utf-8");
		this.clientStringWriter = new ClientStringWriter(
				socketStreamData.getBfWriter());
		

		this.kaibesFilesUtil = new KaibesFilesUtil(5);
	}

	// 登陆监听线程
	public void createThreadLogin(int num) {
		for (int i = 0; i < num; i++) {
			new Thread(new ClientStringReaderLogin(this,
					socketStreamData.getBfReader())).start();
			currentThreadNum++;
		}
	}

	// 主监听线程
	public void createThreadMain(int num) {
		for (int i = 0; i < num; i++) {
			new Thread(new ClientStringReaderMain(this,
					socketStreamData.getBfReader())).start();
			currentThreadNum++;
		}
	}
	
	public void unLogin(){
		synchronized (currentThreadNum) {
			currentThreadNum--;
			if (currentThreadNum <= 0) {
				if (userCenterData != null) {
					ResultSet result = KaibesimcenterHelper.getInstance()
							.getContactname(userCenterData.getUsername());

					if (result != null) {
						try {
							while (result.next()) {
								String contactname = result
										.getString("contactname");

								UserSocketControl toClient = null;
								if ((toClient = KaibesIMServer.mainClientControl
										.getMainSocketControl(contactname)) != null) {
									JsonParserUtil toFriend = new JsonParserUtil(
											1, 7);

									toFriend.putJsonObjectSonValue(
											"contactname",
											userCenterData.getUsername())
											.putJsonObjectSonValue("online",
													false)
											.commit(toClient
													.getClientStringWriter());
								}

							}

						} catch (SQLException e) {
							e.printStackTrace();
						}
					}

					KaibesIMServer.mainClientControl
							.removeMainSocketControl(userCenterData
									.getUsername());

				}
				getKaibesFilesUtil().clean();
			}
		}
	}

	public void closeThreadSocket() {
		synchronized (currentThreadNum) {
			currentThreadNum--;
			if (currentThreadNum <= 0) {
				if (userCenterData != null) {
					if (StartButtonListenerModel.flag) {
						ResultSet result = KaibesimcenterHelper.getInstance()
								.getContactname(userCenterData.getUsername());
						
						if (result != null) {
							try {
								while (result.next()) {
									String contactname = result
											.getString("contactname");
									
									UserSocketControl toClient = null;
									if ((toClient = KaibesIMServer.mainClientControl
											.getMainSocketControl(contactname)) != null) {
										JsonParserUtil toFriend = new JsonParserUtil(
												1, 7);
										
										toFriend.putJsonObjectSonValue(
												"contactname",
												userCenterData.getUsername())
												.putJsonObjectSonValue("online",
														false)
														.commit(toClient
																.getClientStringWriter());
									}
									
								}
								
							} catch (SQLException e) {
								e.printStackTrace();
							}
						}
					}

					KaibesIMServer.mainClientControl
							.removeMainSocketControl(userCenterData
									.getUsername());

				}
				getKaibesFilesUtil().clean();
				KaibesIMServer.mainClientControl.removeClient(client
						.getInetAddress().toString());
				if (!client.isClosed()) {
					socketStreamData.closeStream();
				}
			}
		}
	}

	public void removeCurrentThreadNum() {
		currentThreadNum--;
	}

	public Socket getClient() {
		return client;
	}

	public UserCenterInformationData getUserCenterData() {
		return userCenterData;
	}

	public void setUserCenterData(UserCenterInformationData userCenterData) {
		this.userCenterData = userCenterData;
		setUserFileFolder(new File(StaticValuesUtil.ROOTFILE_FILES,userCenterData.getUsername()));
		setUserCacheFolder(new File(StaticValuesUtil.ROOTFILE_CACHE,userCenterData.getUsername()));
	}

	public void setClient(Socket client) {
		this.client = client;
	}

	public SocketStreamData getSocketStreamData() {
		return socketStreamData;
	}

	public void setSocketStreamData(SocketStreamData socketStreamData) {
		this.socketStreamData = socketStreamData;
	}

	public ClientStringWriter getClientStringWriter() {
		return clientStringWriter;
	}

	public void setClientStringWriter(ClientStringWriter clientStringWriter) {
		this.clientStringWriter = clientStringWriter;
	}

	public File getUserFileFolder() {
		return userFileFolder;
	}

	private void setUserFileFolder(File userFileFolder) {
		this.userFileFolder = userFileFolder;
		if (!this.userFileFolder.exists()) {
			this.userFileFolder.mkdirs();
		}
	}

	public File getUserCacheFolder() {
		return userCacheFolder;
	}

	private void setUserCacheFolder(File userCacheFolder) {
		this.userCacheFolder = userCacheFolder;
		if (!this.userCacheFolder.exists()) {
			userCacheFolder.mkdirs();
		}
	}

	public KaibesFilesUtil getKaibesFilesUtil() {
		return kaibesFilesUtil;
	}

}
